package com.faye.sdk.service;

import io.modelcontextprotocol.server.McpServer;
import io.modelcontextprotocol.server.McpServerFeatures;
import io.modelcontextprotocol.server.McpSyncServer;
import io.modelcontextprotocol.server.transport.WebMvcSseServerTransportProvider;
import io.modelcontextprotocol.spec.McpSchema;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import lombok.RequiredArgsConstructor;

//@Service
@RequiredArgsConstructor
public class McpSyncService {
    private final WebMvcSseServerTransportProvider webMvcSseServerTransportProvider;

    private McpSyncServer mcpSyncServer;

    @PostConstruct
    public void init() {
        mcpSyncServer = McpServer.sync(webMvcSseServerTransportProvider)
            .serverInfo("my-server", "1.0.0")
            .capabilities(McpSchema.ServerCapabilities.builder()
                .resources(false, true)  // Enable resource support
                .tools(true)             // Enable tool support
                .prompts(false)           // Enable prompt support
                .logging()               // Enable logging support
                .completions()           // Enable completions support
                .build())
            .build();

        // Register tools, resources, and prompts
        mcpSyncServer.addTool(createSyncToolSpecification());
    }

    public McpServerFeatures.SyncToolSpecification createSyncToolSpecification() {
        var schema = """
            {
              "type" : "object",
              "id" : "urn:jsonschema:Operation",
              "properties" : {
                "operation" : {
                  "type" : "string"
                },
                "a" : {
                  "type" : "number"
                },
                "b" : {
                  "type" : "number"
                }
              }
            }
            """;
        return new McpServerFeatures.SyncToolSpecification(
            new McpSchema.Tool("calculator", "Basic calculator", schema),
            (exchange, arguments) -> {
                // Tool implementation
                return new McpSchema.CallToolResult("3", false);
            }
        );
    }

    @PreDestroy
    public void destroy() {
        mcpSyncServer.close();
    }
}
